package com.example.testplanetorbit

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PointF

/**
 * 外层轨道，轨道上有2个标记点
 */
class SecondOrbit : OrbitData() {

    var tagDot : OvalTagDot? = null
    var tagDot2 : OvalTagDot? = null
    var tagDots = arrayListOf<OvalTagDot>()

    var textPaint:Paint? = null

    var MAX_TEXT_SIZE = 36f
    var MIN_TEXT_SIZE = 24f
    var TEXT_STROKE_WIDTH = 12f

    override fun init(ovalConfig: OvalConfig) {
        super.init(ovalConfig)

        if (textPaint == null) {
            textPaint = Paint()
            textPaint?.isAntiAlias = true
            textPaint?.color = Color.parseColor("#AAAAAA")
            textPaint?.style = Paint.Style.FILL
            textPaint?.strokeWidth = TEXT_STROKE_WIDTH
            textPaint?.textSize = MAX_TEXT_SIZE
        }

        if (ovalConfig.tags.isNotEmpty()) {
            tagDot = OvalTagDot(ovalConfig.tags[0])
            tagDot2 = OvalTagDot(ovalConfig.tags[1])
//            for (tag in ovalConfig.tags) {
//                val tagDot = OvalTagDot(tag)
//                tagDot.init(true)
//                tagDots.add(tagDot)
//            }
        }

        tagDot?.init(true)
        tagDot2?.init(true)

        when(ovalConfig?.circle) {
         2 -> {
             tagDot?.initDistance(0.25f)
             tagDot2?.initDistance(0.75f)
//             val tagCount = tagDots.size
//             for (dot in tagDots) {
//
//             }
         }
         3 -> {
             tagDot?.initDistance(0.55f)
             tagDot2?.initDistance(0.23f)
         }
         4 -> {
             tagDot?.initDistance(0.87f)
             tagDot2?.initDistance(0.15f)
         }
         5 -> {
             tagDot?.initDistance(0.75f)
             tagDot2?.initDistance(0.35f)
         }
        }
    }


    var maxHR : Float = 0f
    var minHR : Float = 0f
    var maxVR : Float = 0f
    var minVR : Float = 0f
    override fun optimazeConfig() {
        when (mOvalCofig?.circle) {
            2 -> {
                maxHR = 200f / 360f
                minHR = 184f / 260f
                maxVR = 124f / 360f
                minVR = 100f / 360f
                perspectiveDiff = 3f
            }
            3 -> {
                maxHR = 300f / 360f
                minHR = 276f / 260f
                maxVR = 185f / 360f
                minVR = 150f / 360f
                perspectiveDiff = 17f
            }
            4 -> {
                maxHR = 400f / 360f
                minHR = 370f / 260f
                maxVR = 248f / 360f
                minVR = 200f / 360f
                perspectiveDiff = 30f
            }
            5 -> {
                maxHR = 520f / 360f
                minHR = 480f / 260f
                maxVR = 320f / 360f
                minVR = 260f / 360f
                perspectiveDiff = 50f
            }
        }

        mOvalCofig!!.horizontal_r_max = ((mOvalCofig?.mParentWidth?:0) * maxHR * 0.5f).toInt()
        mOvalCofig!!.horizontal_r_min = ((mOvalCofig?.mParentWidth?:0) * minHR * 0.5f).toInt()
        mOvalCofig!!.horizontal_r = mOvalCofig!!.horizontal_r_max

        mOvalCofig!!.vertical_r_max = ((mOvalCofig?.mParentWidth?:0) * maxVR * 0.5f).toInt()
        mOvalCofig!!.vertical_r_min = ((mOvalCofig?.mParentWidth?:0) * minVR * 0.5f).toInt()
        mOvalCofig!!.vertical_r = mOvalCofig!!.vertical_r_min
    }

    override fun clickDot(point: PointF): OvalTagDot.Data? {
        val click1 = tagDot?.getClickRect(point)
        var data = if (click1 == true) {
            tagDot?.tagData
        } else { null }


        val click2 = tagDot2?.getClickRect(point)
        data = if (click2 == true) {
            tagDot2?.tagData
        }else { null }
        return data
    }

    override fun changeHorizontal(moveLeft: Boolean) {
        tagDot?.changeHorizontal(moveLeft)
        tagDot2?.changeHorizontal(moveLeft)
    }

    override fun drawOval1(canvas: Canvas?) {
        tagDot?.drawOval1(canvas)
        tagDot2?.drawOval1(canvas)
    }

    override fun setStepLock(lock: Boolean) {
        tagDot?.lock = lock
        tagDot2?.lock = lock
    }

    override fun setPathMeasure1() {
        tagDot?.setPath(re21, resetPath)
        tagDot2?.setPath(re21, resetPath)
    }

    override fun drawText(canvas: Canvas?) {
        textPaint?.textSize = (tagDot?.scaleRadio ?: 1f) * (MAX_TEXT_SIZE - MIN_TEXT_SIZE) + MIN_TEXT_SIZE  //控制文字大小的改变
        tagDot?.drawText(mOvalCofig, textPaint, canvas)
        tagDot2?.drawText(mOvalCofig, textPaint, canvas)
    }

}